package io.renren.modules.device.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.annotation.DataFilter;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.device.dao.BuildingDao;
import io.renren.modules.device.entity.BuildingEntity;
import io.renren.modules.device.service.BuildingService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;


@Service("buildingService")
public class BuildingServiceImpl extends ServiceImpl<BuildingDao, BuildingEntity> implements BuildingService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        EntityWrapper<BuildingEntity> query=new EntityWrapper<>();
        if(!ShiroUtils.getUserId().equals(Constant.SUPER_ADMIN)){
            query.and("  dept_id in  (select  d.dept_id from    sys_dept d inner join sys_user u  on d.dept_id=u.dept_id\n" +
                    "where   u.user_id={0} and ( d.parent_id=u.dept_id or d.dept_id=u.dept_id ))",ShiroUtils.getUserId());
        }
        Page<BuildingEntity> page = this.selectPage(
                new Query<BuildingEntity>(params).getPage(),
                query
        );

        return new PageUtils(page);
    }

    @Override
    public Set<String> getBuildingArea(int level, String parentKey) {
        Wrapper<BuildingEntity> wraper = new EntityWrapper<>();
        if (level == 0) {
            wraper.isNotNull("provinces");
        }
        switch (level) {

            case 1:
                wraper.eq(StringUtils.isNotBlank(parentKey), "provinces", parentKey);
                break;
            case 2:
                wraper.eq(StringUtils.isNotBlank(parentKey), "cities", parentKey);
                break;
            case 3:
                wraper.eq(StringUtils.isNotBlank(parentKey), "counties", parentKey);
                break;
        }
//       wraper.eq(StringUtils.isNotBlank(parentKey),"provinces",parentKey)


        List<BuildingEntity> list = this.selectList(wraper);
        Set<String> res = new TreeSet<>();
        for (BuildingEntity buildingEntity : list) {
            switch (level) {
                case 0:
                    res.add(buildingEntity.getProvinces());
                    break;
                case 1:
                    res.add(buildingEntity.getCities());
                    break;
                case 2:
                    res.add(buildingEntity.getCounties());
                    break;
                case 3:
                    res.add(buildingEntity.getBuildingName());
                    break;
            }

        }
        return res;
    }
    
    
    @Override
    public Set<String> getBuildingCodeArea(int level, String parentKey) {
        Wrapper<BuildingEntity> wraper = new EntityWrapper<>();
        if (level == 0) {
            wraper.isNotNull("provinces");
        }
        switch (level) {

            case 1:
                wraper.eq(StringUtils.isNotBlank(parentKey), "provinces", parentKey);
                break;
            case 2:
                wraper.eq(StringUtils.isNotBlank(parentKey), "cities", parentKey);
                break;
            case 3:
                wraper.eq(StringUtils.isNotBlank(parentKey), "counties", parentKey);
                break;
        }
//       wraper.eq(StringUtils.isNotBlank(parentKey),"provinces",parentKey)


        List<BuildingEntity> list = this.selectList(wraper);
        Set<String> res = new TreeSet<>();
        for (BuildingEntity buildingEntity : list) {
            switch (level) {
                case 0:
                    res.add(buildingEntity.getProvinces());
                    break;
                case 1:
                    res.add(buildingEntity.getCities());
                    break;
                case 2:
                    res.add(buildingEntity.getCounties());
                    break;
                case 3:
                    res.add(buildingEntity.getBuildingCode()+"-"+buildingEntity.getBuildingName());
                    break;
            }

        }
        return res;
    }

}
